home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1991 / 06 / herctest.c < prev    next >
Text File  |  1991-03-06  |  3KB  |  138 lines

  1. /* herctest.c: Test routines for the line-drawing routine in
  2.  *    hercline.asm.  Test the line-drawing routine extensively,
  3.  *    using the library routine as the standard, and report on
  4.  *    the relative performance compared to the library routine.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <graphics.h>
  10. #include <stdlib.h>
  11. #include <dos.h>
  12. #include <time.h>
  13. #include <math.h>
  14.  
  15. /* Number of repetitions to do in the beginning */
  16. #define NUM_REPS 1000
  17.  
  18. extern void hercline(int x1, int y1, int x2, int y2, int clr);
  19.  
  20. /* Routine to calculate the number of hundredths of seconds in
  21.    a given time structure. */
  22. long
  23. num_hunds(struct time *a)
  24. {
  25.   long hrs, mins, secs;
  26.   hrs = a->ti_hour * 360000L;
  27.   mins = a->ti_min * 6000L;
  28.   secs = a->ti_sec * 100L;
  29.   return(hrs + mins + secs + (long) a->ti_hund);
  30. }
  31.  
  32. /* Routine to calculate the number of hundredths of seconds
  33.    between two given time structures. */
  34. long
  35. diff_time(struct time *beg, struct time *end)
  36. {
  37.   return(num_hunds(end) - num_hunds(beg));
  38. }
  39.  
  40. /* Empty routine.  Used by do_test(). */
  41. void
  42. just_return(int x1, int y1, int x2, int y2, int clr)
  43. {
  44.   return;
  45. }
  46.  
  47. void
  48. do_test(int which)
  49. {
  50.   int x1, y1, x2, y2, clr;
  51.   long i;
  52.  
  53.   for (i = 0; i < NUM_REPS; i++) {
  54.     x1 = rand() % 720;
  55.     y1 = rand() % 348;
  56.     x2 = rand() % 720;
  57.     y2 = rand() % 348;
  58.     switch(which) {
  59.       case 0:
  60.     just_return(x1, y1, x2, y2, clr);
  61.     break;
  62.       case 1:
  63.     hercline(x1, y1, x2, y2, 1);
  64.     break;
  65.       case 2:
  66.     line(x1, y1, x2, y2);
  67.     break;
  68.       case 3:
  69.     hercline(x1, y1, x2, y2, 0);
  70.       default:
  71.     break;
  72.     }
  73.   }
  74. }
  75.  
  76. void main(void)
  77. {
  78.   int dr, md;
  79.   char cmd;
  80.   struct time beg, end;
  81.   long hunds_blank, hunds_lib, hunds_newline;
  82.   int i;
  83.   int seed;
  84.   long now;
  85.   long len;
  86.  
  87.   dr = md = 0;
  88.   registerbgidriver(Herc_driver);
  89.   initgraph(&dr, &md, "C:\LIB");
  90.  
  91.   seed = time(&now) % 37;
  92.  
  93. /* Time empty loop */
  94.   srand(seed);    gettime(&beg);    do_test(0);  gettime(&end);
  95.   hunds_blank = diff_time(&beg, &end);
  96.  
  97. /* Time our assembler routine */
  98.   srand(seed);    gettime(&beg);    do_test(1);  gettime(&end);
  99.   hunds_newline = diff_time(&beg, &end);
  100.  
  101. /* Time library routine */
  102.   srand(seed);    gettime(&beg);    do_test(2);  gettime(&end);
  103.   hunds_lib = diff_time(&beg, &end);
  104.   srand(seed);
  105.   do_test(3);
  106.  
  107.   restorecrtmode();
  108.   printf("Dead time: %ld\n", hunds_blank);
  109.   printf("New line: %ld\n", hunds_newline);
  110.   printf("Library routine: %ld\n", hunds_lib);
  111.   hunds_newline -= hunds_blank;
  112.   hunds_lib -= hunds_blank;
  113.   printf("New line is %.2f times as fast as library routine\n",
  114.      (float) hunds_lib / hunds_newline);
  115.   printf("(hit any key to continue)\n");
  116.   cmd = getch();
  117.   setgraphmode(md);
  118.   for (i = 0; i < 347; i++) {
  119.     hercline(347-i, 0, 0, i, 1);
  120.     hercline(347-i, 347, 0, 347-i, 1);
  121.     hercline(371+i, 0, 719, i, 1);
  122.     hercline(371+i, 347, 719, 347-i, 1);
  123.   }
  124.   /* Fill screen */
  125.   for (i = 0; i < 347; i++)
  126.     hercline(0, i, 719, i, 1);
  127.  
  128.   for (i = 0; i < 347; i++) {
  129.     hercline(347-i, 0, 0, i, 0);
  130.     hercline(347-i, 347, 0, 347-i, 0);
  131.     hercline(371+i, 0, 719, i, 0);
  132.     hercline(371+i, 347, 719, 347-i, 0);
  133.   }
  134.   for (i = 0; i < 347; i++)
  135.     hercline(0, i, 719, i, 0);
  136.   closegraph();
  137. }
  138.